import time
from threading import Lock, Thread, current_thread

ticket = 50
lock = Lock()


def sale():
    global ticket
    for _ in range(50):
        lock.acquire()
        if ticket > 0:
            print(current_thread().name, "正在出售第%d张票" % ticket)
            ticket -= 1

        lock.release()
        # 演示使用
        time.sleep(1)

if __name__ == '__main__':
    lst = []
    # 4个窗口线程一块卖票
    for i in range(4):
        lst.append(Thread(target=sale))
    # 启动线程，开始售卖
    for i in lst:
        i.start()
    # 堵塞线程，等待所有线程结束
    for i in lst:
        i.join()
    print("执行完毕")
